package com.base.cn.platform.os.controller.index;

import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.utils.action.ActionKey;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.common.utils.web.WebUtils;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.entity.planTools.PopupSlogan;
import com.base.cn.platform.os.entity.user.integral.IntegralRule;
import com.base.cn.platform.os.service.course.catalog.CourseCatalogService;
import com.base.cn.platform.os.service.course.cou.CourseService;
import com.base.cn.platform.os.service.course.open.CourseOpenService;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import com.base.cn.platform.os.service.manage.op.groom.OpGroomDataService;
import com.base.cn.platform.os.service.manage.planTools.PopupSloganDetailService;
import com.base.cn.platform.os.service.manage.planTools.PopupSloganService;
import com.base.cn.platform.os.service.manage.subject.SubjectService;
import com.base.cn.platform.os.service.manage.user.integral.CusUserIntegralRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * App首页接口Controller
 *
 * @author s.li
 * @date 2018/11/01
 */
@RestController
@RequestMapping("/api/index/")
public class ApiIndexController extends ApiBaseController {

    @Autowired
    private OpGroomDataService opGroomDataService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseOpenService courseOpenService;
    @Autowired
    private CusUserIntegralRuleService cusUserIntegralRuleService;
    @Autowired
    private CourseCatalogService courseCatalogService;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private PopupSloganService popupSloganService;
    @Autowired
    private PopupSloganDetailService popupSloganDetailService;


    /**
     * 获取首页数据第一个接口
     *
     * @return Map<String       ,       Object>
     */
    @RequestMapping("/indexDataOne")
    public Map<String, Object> noticeList() {
        //首页资讯推荐
        Map<String, Object> params = new HashMap<>();
        params.put("placeKey", "IPA");
        List<Map<String, Object>> articleList = opGroomDataService.queryGroomDataList(params);
        //首页专业推荐
        List<Map<String, Object>> subjectList = subjectService.findH5IndexSubjectList();
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("articleList", articleList);
        dataMap.put("subjectList", subjectList);
        return ResultUtil.SUCCESS(dataMap);
    }


    /**
     * 获取首页数据第二个接口
     *
     * @return Map<String       ,       Object>
     */
    @RequestMapping("/indexDataTwo")
    public Map<String, Object> hotLiveCourseList() {
        //获取热门直播
        List<Map<String, Object>> hotLiveCourseList = opGroomDataService.findAppH5IndexGroomData("AHPL");
        //小班课
        List<Map<String, Object>> smallCourseList = opGroomDataService.findAppH5IndexGroomData("SMALl");
        //精选课程
        List<Map<String, Object>> choiceCourseList = opGroomDataService.findAppH5IndexGroomData("IBC");
        //热闹课程
        List<Map<String, Object>> hotCourseList = opGroomDataService.findAppH5IndexGroomData("IHC");
        //查询首页专业推荐的课程
        List<Map<String, Object>> subjectCourseList = courseService.findRecommendCourseList("AHBSC", true, true);

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("hotLiveCourseList", hotLiveCourseList);
        dataMap.put("choiceCourseList", choiceCourseList);
        dataMap.put("hotCourseList", hotCourseList);
        dataMap.put("subjectCourseList", subjectCourseList);
        dataMap.put("smallCourseList", smallCourseList);
        return ResultUtil.SUCCESS(dataMap);
    }

    /**
     * 首页今日直播数据接口
     *
     * @param userId 用户ID（未登录传0）
     * @return Map<String       ,       Object>
     */
    @RequestMapping("/queryOpenCourseList")
    public Map<String, Object> queryOpenCourseList(@RequestParam("userId") BigDecimal userId) {
        Map<String, Object> condition = new HashMap<>();
        condition.put("userId", userId);
        condition.put("count", 2);
        condition.put("openLive", 1);
        condition.put("status", 1);
        List<Map<String, Object>> courseOpenList = courseOpenService.findCourseOpenList(condition, false, false, true, true);
        return ResultUtil.SUCCESS(courseOpenList);
    }

    /**
     * 获取积分别名
     *
     * @return
     */
    @RequestMapping("/getIntegralAlias")
    public Map<String, Object> getIntegralAlias(@RequestParam(value = "userId", required = false, defaultValue = "0") BigDecimal userId) {
        IntegralRule userIntegralRuleByKey = cusUserIntegralRuleService.getUserIntegralRuleByKey(ActionKey.NAME);
        TreeMap<String, String> contextTreeMap = userIntegralRuleByKey.getContextTreeMap();
        TreeMap<String, String> sysConfigContext = sysConfigService.getSysConfigContext(ConfigName.SERVICE);
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        Integer count = courseCatalogService.queryCurrentCount(params);
        contextTreeMap.put("notLive", count > 0 ? "true" : "false");
        contextTreeMap.putAll(sysConfigContext);
        return ResultUtil.SUCCESS(contextTreeMap);
    }

    /**
     * 获取是否有弹框
     */
    @RequestMapping("/getIndexPopupSlogan")
    @ResponseBody
    public Map getPopupSlogan(@RequestParam(value = "mobileId") String userId) {
        Map<String, Object> resultMap = popupSloganService.getIndexPopupSlogan("mobile", userId);
        Map<String, Object> map = new HashMap<>();
        if ((boolean)resultMap.get("success")) {
            LinkedHashMap popupSlogan = (LinkedHashMap) resultMap.get("entity");
            String popupSloganDetailId = popupSlogan.get("popupSloganDetailId").toString();
            String imag = popupSlogan.get("imag").toString();
            String type = "";
            String courseId = "";
            String chained = "";
            String courseType = "";
            if (!"0".equals(popupSlogan.get("chainedType").toString())) {
                if (popupSlogan.get("chained").toString().contains("courseId")) {
                    type = "1";
                    courseId = popupSlogan.get("chained").toString().split("=")[1].replace("#","");
                    Map<String, Object> courseMap = courseService.findCourseById(new BigDecimal(courseId), false, false, false, false);
                    courseType = courseMap.get("courseTypeKey").toString();
                } else {
                    type = "2";
                    chained = popupSlogan.get("chained").toString();
                }
            } else {
                type = "0";
            }
            map.put("type", type);
            map.put("imag", imag);
            map.put("courseId", courseId);
            map.put("chained", chained);
            map.put("courseType", courseType);
            map.put("popupSloganDetailId", popupSloganDetailId);
            return ResultUtil.SUCCESS(map);
        } else {
            return ResultUtil.ERROR("");
        }
    }

    /**
     * 添加弹框点击
     * @param id
     * @return
     */
    @RequestMapping("/addClick")
    @ResponseBody
    public Map addClick(@RequestParam("popupSloganDetailId") BigDecimal id) {
        popupSloganDetailService.updateClick(id);
        return ResultUtil.SUCCESS("");
    }

}
